package com.zm.zmgz.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: cjw
 * @Date: 2020/03/26/2:17
 * @Description: 日期相关工具类
 */
public class DateUtils {

    public static String DATEFORMAT = "yyyy-MM-dd HH-mm-ss.SSS";

    public static String DATEFORMAT2 = "yyyy年M月d日";

    public static String DATEFORMAT3 = "yyyy-MM-dd HH-mm-ss";

    public static void main(String[] args) {
        Date date = getLastMonthDate(new Date());
        String string = getDateString(date, "yyyy年MM月");
        System.err.println(string);
    }


    /***
    * @Description: 从传入date中获取上月date
    * @Param:
    * @return:
    * @Author: cjw
    * @Date: 2020-05-29 10:27:06
    */
    public static Date getLastMonth(Date date) {
        if(Objects.isNull(date))
            return null;
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH,-1);
        return calendar.getTime();
    }


    /***
     * @Description: 将传入字符串转化为date类型
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-22 14:04:37
     */
    public static Date getDateByString(String date, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        Date date1 = new Date();
        try {
            date1 = simpleDateFormat.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date1;
    }

    /***
     * @Description: 判断两个日期是否在同一个月份
     * 如果两个日期的月份开始时间一致,则两个日期在同一月份
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-13 12:24:07
     */
    public static boolean isSameMonth(Date date, Date date2) {
        Date monthStart = DateUtils.getMonthStart(date);
        Date monthStart2 = DateUtils.getMonthStart(date2);
        if (monthStart.equals(monthStart2))
            return true;
        else
            return false;
    }

    /***
     *
     * @Description: 根据传入的时间和时间格式字符串 返回相应的时间字符串
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-03-26 02:23:58
     */
    public static String getDateString(Date date, String dateFormat) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(dateFormat);
        return simpleDateFormat.format(date);
    }


    /***
     * @Description: 返回传入日期所在月的开始时间
     * 示例:2020-03-01 00-00-00.000
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-03-26 02:20:43
     */
    public static Date getMonthStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        calendar.set(calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /***
     * @Description: 返回传入日期所在月的结束时间
     * 示例:2020-03-31 23-59-59.999
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-03-26 02:20:43
     */
    public static Date getMonthEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, 1);
        calendar.set(Calendar.DAY_OF_MONTH, 0);
        calendar.set(calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }


    /***
     * @Description: 返回传入月份的上一个月份的时间
     * @Param:
     * @return:
     * @Author: cjw
     * @Date: 2020-05-19 14:10:57
     */
    @Deprecated
    public static Date getLastMonthDate(Date date) {
        Date monthStart = getMonthStart(date);
        return new Date(monthStart.getTime() - 1);
    }
}
